//
//  FuLiViewController.m
//  jingjing2.0
//
//  Created by apple on 16/4/27.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "FuLiViewController.h"
#import "HotActivityCell.h"
#import "HotActivityData.h"
#import "WelfareData.h"
#import "WelfareCell.h"
#import "PinDanViewController.h"
#import "ActivityCenterViewController.h"
#import "BusinessWelfareViewController.h"
#import "InviteViewController.h"
#import "ColorfulEggViewController.h"
@interface FuLiViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)AnimationView *animation;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)NSMutableArray *hotSource;

@end

@implementation FuLiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"福利中心";
    self.view.backgroundColor=[UIColor whiteColor];
    _animation=[AnimationView sharedAnimationView];
    _animation.center=self.view.center;
    self.dataSource=[NSMutableArray array];
    self.hotSource=[NSMutableArray array];
    [self.view addSubview:_animation];
    [self setUpTableView];
    [self getHotActivity];
}
#pragma mrak---表
-(void)setUpTableView
{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kWidth, kHeight-64-50) style:UITableViewStyleGrouped];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    [_tableView registerNib:[UINib nibWithNibName:@"HotActivityCell" bundle:nil] forCellReuseIdentifier:@"hot"];
    [_tableView registerNib:[UINib nibWithNibName:@"WelfareCell" bundle:nil]forCellReuseIdentifier:@"welfare"];
    [self.view addSubview:_tableView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0 || section==1) {
        return 10;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==0 || section==1) {
        return 10;
    }
    return 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 2;
    }else if (section==1)
    {
        return 3;
    }else
    {
       return self.hotSource.count;
    }
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section==2) {
        return @"热门活动";
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==2) {
        return 150;
    }
    return 44;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==2) {
        HotActivityCell *hot=[_tableView dequeueReusableCellWithIdentifier:@"hot" forIndexPath:indexPath];
        hot.selectionStyle=UITableViewCellAccessoryNone;
        HotActivityData *data=self.hotSource[indexPath.row];
        [hot.image sd_setImageWithURL:[NSURL URLWithString:data.appIndexImage] placeholderImage:nil options:SDWebImageRetryFailed];
        hot.time.text=data.RemainderTime;
        hot.peopleNumber.text=[NSString stringWithFormat:@"已有%ld人参与",(long)data.ParticipateNum];
        return hot;
    }else
    {
        WelfareCell *welfare=[_tableView dequeueReusableCellWithIdentifier:@"welfare" forIndexPath:indexPath];
        welfare.selectionStyle=UITableViewCellAccessoryNone;
        WelfareData *data=nil;
        if (self.dataSource.count>0) {
            if (indexPath.section==0) {
                data=self.dataSource[indexPath.row];
            }else
            {
                data=self.dataSource[indexPath.row+2];
            }
            [welfare.image sd_setImageWithURL:[NSURL URLWithString:data.IconUrl] placeholderImage:nil options:SDWebImageRetryFailed];
            welfare.title.text=data.CategoryName;
            welfare.remark.text=data.Remark;
            welfare.tag=data.CategoryID;
        }
        
        return welfare;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==2) {
        //进入网页
        HotActivityData *data=self.hotSource[indexPath.row];
        if (data.IsStatic==1) {
            WebViewController *webView=[[WebViewController alloc]init];
            webView.title=data.Title;
            webView.pageUrl=data.PageUrl;
            [self.navigationController pushViewController:webView animated:YES];
        }

    }else
    {
        WelfareCell *welfare=[_tableView cellForRowAtIndexPath:indexPath];
        if ([welfare.title.text isEqualToString:@"拼单"]) {
            PinDanViewController *pindan=[[PinDanViewController alloc]init];
            pindan.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:pindan animated:YES];
        }else if ([welfare.title.text isEqualToString:@"活动中心"])
        {
            ActivityCenterViewController *center=[[ActivityCenterViewController alloc]init];
            center.title=@"活动中心";
            center.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:center animated:YES];
        }else if ([welfare.title.text isEqualToString:@"邀请返利"])
        {
            InviteViewController *invite=[[InviteViewController alloc]init];
            invite.title=@"净净洗衣邀请返利";
            invite.categoryID=[NSString stringWithFormat:@"%ld",welfare.tag];
            invite.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:invite animated:YES];
        }else if ([welfare.title.text isEqualToString:@"企业福利"])
        {
            BusinessWelfareViewController *bussiness=[[BusinessWelfareViewController alloc]init];
            bussiness.hidesBottomBarWhenPushed=YES;
            
            [self.navigationController pushViewController:bussiness animated:YES];
        }else if ([welfare.title.text isEqualToString:@"彩蛋"])
        {
            ColorfulEggViewController *egg=[[ColorfulEggViewController alloc]init];
            egg.title=@"彩蛋";
            egg.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:egg animated:YES];
            
        }

    }
}
#pragma mark--网络请求之获取数据
-(void)getHotActivity
{
    //http://192.168.1.107:3722/Activity/ActList/{UserCode/{VerNumber}/{CityCode}/{ClientType}/{IsHot}/{Timestamp}/{Sign}
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSDictionary *signDic=@{@"UserCode":GETUSERCODE,@"VerNumber":VERNUMBER,@"CityCode":GETCITYCODE,@"ClientType":CLIENTTYPE,@"IsHot":@"1",@"Timestamp":timeStamp};
    NSString *sign=[GeneralTool GetSin:signDic];
    NSString *urlString=[NSString stringWithFormat:@"Activity/ActList/%@/%@/%@/%@/%@/%@/%@",GETUSERCODE,VERNUMBER,GETCITYCODE,CLIENTTYPE,@"1",timeStamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"网络请求超时"];
        }else
        {
            if ([responses[@"IsSuccess"]intValue]==1) {
                [_animation.imageView stopAnimating];
                _animation.hidden=YES;
                NSDictionary *dic=responses[@"ReturnObject"];
                    NSArray *keys=[dic allKeys];
                    for (NSString *str in keys) {
                        if ([str isEqualToString:@"HotActList"])
                        {
                            NSArray *dataArr=dic[@"HotActList"];
                            if (dataArr!=[NSNull null]) {
                                for (NSDictionary *datadic in dataArr)
                                {
                                    HotActivityData *data=[[HotActivityData alloc]initWithDic:datadic];
                                    [self.hotSource addObject:data];
                                }

                            }
                        }else
                        {
                            NSArray *dataArr=dic[@"WelfareHomeList"];
                            for (NSDictionary *datadic in dataArr)
                            {
                                WelfareData *data=[[WelfareData alloc]initWithDic:datadic];
                                [self.dataSource addObject:data];
                            }
                        }
                    }
                    [_tableView reloadData];
                   [MBProgressHUD hideHUDForView:self.view];
                }
        }
    }];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    _animation.hidden=NO;
    [_animation.imageView startAnimating];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    [_animation.imageView stopAnimating];
    _animation.hidden=YES;
}
//viewwillAppear要刷新一次数据，数据请求成功后隐藏动画
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
